package bg.btanev.eightyfour.client.view;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import bg.btanev.eightyfour.client.composite.ExpandableListElementComposite;
import bg.btanev.eightyfour.client.presenter.listener.ActionMenuOpenListener;
import bg.btanev.eightyfour.client.presenter.listener.TargetActionMenuOpenListener;
import bg.btanev.eightyfour.shared.dto.TargetDto;
import bg.btanev.eightyfour.shared.dto.person.PersonDto;
import bg.btanev.eightyfour.shared.dto.person.PersonNameDto;
import bg.btanev.eightyfour.shared.dto.target.TargetStatusDto;

import com.google.gwt.dom.client.Style.Display;
import com.google.gwt.dom.client.Style.Float;
import com.google.gwt.dom.client.Style.FontWeight;
import com.google.gwt.dom.client.Style.VerticalAlign;
import com.google.gwt.event.logical.shared.HasOpenHandlers;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Label;

/**
 *
 * @author B.Tanev
 *
 */
public class TargetListElementView extends ExpandableListElementComposite {
	private final Label titleCaption;
	private final Label dateCaption;
	private final TargetListElementsContainerView targetDetailsContainer;

	private TargetDto target;

	private Set<TargetActionMenuOpenListener> actionMenuOpenListeners = new HashSet<TargetActionMenuOpenListener>();

	public TargetListElementView() {
		final FlowPanel flowPanel = new FlowPanel();
		flowPanel.setWidth("100%");
		flowPanel.getElement().getStyle().setVerticalAlign(VerticalAlign.MIDDLE);
		flowPanel.getElement().getStyle().setDisplay(Display.INLINE_BLOCK);
		getHeaderPanel().add(flowPanel);

		titleCaption = new Label("Title");
		titleCaption.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
		titleCaption.setWordWrap(true);
		titleCaption.setWidth("900px");
		titleCaption.getElement().getStyle().setFontWeight(FontWeight.BOLDER);
		titleCaption.getElement().getStyle().setFloat(Float.LEFT);
		titleCaption.getElement().getStyle().setDisplay(Display.INLINE);
		flowPanel.add(titleCaption);

		dateCaption = new Label("");
		dateCaption.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
		dateCaption.setWidth("200px");
		dateCaption.setWordWrap(false);
		dateCaption.getElement().getStyle().setFloat(Float.RIGHT);
		dateCaption.getElement().getStyle().setDisplay(Display.INLINE);
		dateCaption.getElement().getStyle().setFontWeight(FontWeight.BOLD);
		flowPanel.add(dateCaption);

		HasWidgets parentDataPanel = super.getDataPanel();
		targetDetailsContainer = new TargetListElementsContainerView();
		parentDataPanel.add(targetDetailsContainer);
		targetDetailsContainer.addActionMenuListener(new ActionMenuOpenListener() {

			@Override
			public void onOpen(String menu) {
				if (menu != null && target != null) {
					notifyTargetActionContainerOpen(menu, target);
				}
			}
		});
	}

	public void addTargetActionMenuOpenListener(TargetActionMenuOpenListener listener) {
		if (listener != null) {
			actionMenuOpenListeners.add(listener);
		}
	}

	protected void notifyTargetActionContainerOpen(String menu, TargetDto target) {
		for (TargetActionMenuOpenListener listener : actionMenuOpenListeners) {
			listener.onOpen(menu, target, targetDetailsContainer.getActionDataCenterPanel());
		}
	}

	public TargetDto getTarget() {
		return target;
	}

	public void addActionMenus(String[] actionMenus) {
		targetDetailsContainer.addActionMenus(actionMenus);
	}

	public void setTarget(TargetDto target) {
		this.target = target;

		titleCaption.setText("");
		dateCaption.setText("");

		if (target == null) {
			return;
		}

		PersonDto person = target.getPerson();
		PersonNameDto personName = person.getPersonName();
		TargetStatusDto status = target.getStatus();

		titleCaption.setText(personName.getFirstName() + " " + personName.getLastName());

		Date lastUpdated = status.getLastUpdated();
		if (lastUpdated != null) {
			DateTimeFormat formatter = DateTimeFormat.getMediumDateTimeFormat();
			dateCaption.setText(formatter.format(lastUpdated));
		}
	}

	public HasWidgets getDataPanel() {
		return targetDetailsContainer.getElementsContainer();
	}

	public HasOpenHandlers<DisclosurePanel> getOpenHandlers() {
		return super.getOpenHandlers();
	}
}
